﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using UniverClient.ServiceReference1;

namespace UniverClient
{
    public partial class MainForm : Form
    {
        // proxy object for getting data for university objects from server
        UniverDataServiceClient uClient;

        public MainForm()
        {
            InitializeComponent();

            uClient = new UniverDataServiceClient();            
            ShowItems(0, treeView.Nodes);

            //UObjectFullData data = uClient.GetObjectData(1);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            #region hardcode
            /*
            treeView.Nodes.Add("asd");
            treeView.Nodes.Add("asd");
            treeView.Nodes.Add("asd");
            */
            #endregion
        }

        /// <summary>
        /// Gets data for child objects of current parent node
        /// </summary>
        /// <param name="parentId">Id of the node to ger children for</param>
        /// <returns></returns>
        private UObject[] GetChildObjects(int parentId)
        {
            return new UObject[0];//uClient.GetChildUObjects(parentId);
            //return uClient.GetChildUObjects(parentId);
        }

        /// <summary>
        /// Renders items in tree view
        /// </summary>
        /// <param name="parentId">DB Id of parent node</param>
        /// <param name="nodesCollection">Collection to add child nodes</param>
        private void ShowItems(int parentId, TreeNodeCollection nodesCollection)
        {
            // clear inner nodes - there might be empty node
            nodesCollection.Clear();

            // add new nodes to collection

            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            UObject[] items = GetHardCodeUObject();
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            //UObject[] items = GetChildObjects(parentId);


            foreach (UObject item in items)
            {
                TreeNode node = new TreeNode(item.Name);
                node.Tag = item;
                node.Nodes.Add("");
                node.Nodes[0].Tag = "none";
                nodesCollection.Add(node);
            }            
        }


        private void treeView_ExpandNode(object sender, TreeViewEventArgs e)
        {
            ShowItems(((UObject)e.Node.Tag).UniqueNo, e.Node.Nodes);            
        }

        private void treeView_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                // Select the clicked node
                treeView.SelectedNode = treeView.GetNodeAt(e.X, e.Y);

                if (treeView.SelectedNode != null)
                {
                    редагуватиToolStripMenuItem.Enabled = true;
                    видалитиToolStripMenuItem.Enabled = true;
                }
                else
                {
                    редагуватиToolStripMenuItem.Enabled = false;
                    видалитиToolStripMenuItem.Enabled = false;
                }

                contextMenu.Show(treeView, e.Location);
            }
        }

        private void редагуватиToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            #region hardCode
            // delete this
            /*
            UObjectFullData _t = new UObjectFullData();
            UObjectPropertie _p = new UObjectPropertie();
            _p.FieldData = "askdjfhaskf";
            _p.FriendlyFieldName = "xmvbxcnmcxn";
            _t.Fields = new UObjectPropertie[3];
            _t.Fields[0] = _t.Fields[1] = _t.Fields[2] = _p;
            new Edit(_t).Show();
             * */
            #endregion

            // pureCode, uncomment
            new Edit(GetHardCodeObject()).Show();
             //new Edit(uClient.GetObjectData(((UObject)treeView.SelectedNode.Tag).UniqueNo)).Show();
        }
        private UObject[] GetHardCodeUObject()
        {
            //Random rd = new Random(3);
            UObject[] res = new UObject[rd.Next(1, 15)];
            for (int i = 0; i < res.Length; i++)
            {
                int _tm = rd.Next(1, 5);
                res[i] = new UObject();
                res[i].Name = RandomString(rd.Next(4, 15), true);
                res[i].Major = rd.Next(1, 10);
                res[i].UniqueNo = rd.Next(1, 10);
            }
            return res;
        }

        Random rd = new Random();
        private UObjectFullData GetHardCodeObject()
        {
            //Random rd = new Random(2);
            UObjectFullData _t = new UObjectFullData();
            _t.ClassName = RandomString(rd.Next(4, 15), true);
            _t.Fields = new UObjectPropertie[rd.Next(3, 15)];
            for (int i = 0; i < _t.Fields.Length; i++)
            {
                _t.Fields[i] = new UObjectPropertie();
                _t.Fields[i].FriendlyFieldName = RandomString(rd.Next(4, 15), true);
                _t.Fields[i].FieldType = rd.Next(1, 5);
                _t.Fields[i].FieldData = RandomString(rd.Next(4, 15), true);
                if (_t.Fields[i].FieldType == 4)
                {
                    _t.Fields[i].FieldData = rd.Next(1, 3) == 1 ? "true" : "false";
                }
                if (_t.Fields[i].FieldType == 3)
                {
                    _t.Fields[i].FieldData = DateTime.Now.AddDays((double)rd.Next(1, 50)).AddMinutes((double)rd.Next(1, 1000)).ToString();
                }
            }
            return _t;
        }

        private string RandomString(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            //Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rd.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }
    }
}
